package com.thyc.timer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.thyc.entity.LimitValue;
import com.thyc.entity.SysConfig;
import com.thyc.enums.EquipmentEnum;
import com.thyc.service.LimitValueService;
import com.thyc.service.SourceDataService;
import com.thyc.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Component
@Slf4j
public class BackUp {

    @Resource
    private SourceDataService sourceDataService;

    @Resource
    private LimitValueService limitValueService;

    @Resource
    private SysConfigService configService;

    /**
     * cmd exe执行备份  已不用
     */
//    @Scheduled(fixedDelay = 1000 * 60 * 30)
//    @Deprecated
//    public void cmdBackUp() {
//        int count = sourceDataService.count();
//        String today = DateUtil.today();
//        if (count > 10000000) {
//            String[] cmd = {"cmd", "/C", "C:\\mysql-8.0.23-winx64\\bin\\mysqldump --defaults-extra-file=C:\\mysql-8.0.23-winx64\\my.ini sm jk_source_data > C:\\sourcedata\\sourcedata" + today + ".sql"};
//            RuntimeUtil.exec(cmd);
//            checkAndRemove(today);
//        }
//        log.info("行数={}", count);
//    }

    /**
     * 1每天凌晨0点备份 当天最大值和最小值
     * 2重置短信发送记录
     */
    @Scheduled(cron = "0 0 0 * * ?")
    @Async
    public void backUpMaxAndMinValue() {
        List<LimitValue> limitValues = sourceDataService.selectMaxAndMinValue();
        limitValues.forEach(v -> v.setAddTime(DateUtil.yesterday()));
        limitValueService.saveBatch(limitValues);
        configService.update(new LambdaUpdateWrapper<SysConfig>()
                .set(SysConfig::getConfigValue,0)
                .eq(SysConfig::getConfigKey,"jk.sendInfoIs"));
    }

    /**
     * mysql 指令备份  每天凌晨12点15
     */
    @Scheduled(cron = "0 15 0 * * ?")
    @Async
    public void backUp() {
        String today = DateUtil.format(DateUtil.yesterday(),"yyyyMMdd");
        for (EquipmentEnum value : EquipmentEnum.values()) {
            String sensorModel = value.getSensorModel();
            sourceDataService.backUp(today,sensorModel);
            ThreadUtil.safeSleep(1000 * 5);
        }
        checkAndRemove(today);
    }

    void checkAndRemove(String today) {
        Boolean flag = true;
        for (EquipmentEnum value : EquipmentEnum.values()) {
            String fileName = today + "(" + value.getSensorModel() + ")";
            File file = new File("C:\\sourcedata\\" + fileName + ".cvs");
            if (!file.exists() || file.length() < 1024) {
                log.info(value.getChannelNum() + "备份失败");
                flag = false;
            } else {
                log.info("{}数据备份成功，日期:{},文件大小:{}",value.getChannelNum(), today, getSize(file.length()));
            }
        }
        if (flag) {
            sourceDataService.truncate();
        }
    }

    String getSize(Long size) {
        if (size < 1024) {
            return size + "B";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            return size + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            size = size * 100;
            return size / 100 + "." + size % 100 + "MB";
        } else {
            size = size * 100 / 1024;
            return size / 100 + "." + size % 100 + "GB";
        }
    }

}
